package reflect;

import java.util.Scanner;

/**
 * 通过反射机制实例化对象
 * 1.使用无参实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入类名:");
        String name = sc.nextLine();
        Class cls = Class.forName(name);  //得类对象


        //假设cls为String的类对象，则newInstance()的结果相当于String obj = new String();
        //假设cls为ArrayList的类对象，则newInstance()的结果相当于ArrayList obj = new ArrayList();
        Object obj = cls.newInstance();  //只能使用无参构造器实例化！
        System.out.println(obj);
    }
}
